package cn.myit.controller;

import cn.myit.domain.Teacher;
import cn.myit.service.ITeacherService;
import cn.myit.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService iTeacherService;

    /**
     * 查询全部教师信息
     */
    @GetMapping("/all")
    public R<List<Teacher>> qureyall() {
        List<Teacher> list = iTeacherService.qureyall();
        return R.ok(list);
    }

    /**
     * 新增老师
     */
    @PostMapping("/add")
    public R inert(@RequestBody Teacher teacher) {
        try {
            iTeacherService.add(teacher);
            return R.ok("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("添加失败");
        }
    }

    /**
     * 修改老师
     */
    @PostMapping("/update")
    public R update(@RequestBody Teacher teacher){
        iTeacherService.update(teacher);
        return R.ok("修改成功");
    }
    /**
     * 删除老师
     */
    @DeleteMapping("/{id}")
    public R remove(@PathVariable("id") Long id){
        try {
            iTeacherService.remove(id);
            return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("删除失败");
        }
    }
}
